
//
//  HLNoteViewController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/15.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLNoteViewController.h"
#import "HLDetailNodeView.h"
#import "HLCreateNoteController.h"
#import "MBProgressHUD+HL.h"
#import "HLUserInfo.h"
#import "HttpRequestManager.h"


@interface HLNoteViewController ()<HLDetailNodeViewDelegate>

@end

@implementation HLNoteViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"笔记";
    self.automaticallyAdjustsScrollViewInsets = 0;
    
    HLDetailNodeView *noteView = [[HLDetailNodeView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) courseID: self.courseID];
    noteView.delegate = self;
    [self.view addSubview:noteView];
}

- (void)detailNodeView:(HLDetailNodeView *)detailNodeView didClickCreteNote:(UIButton *)button
{
    HLCreateNoteController *createNote = [[HLCreateNoteController alloc] init];
    createNote.note_course_id = self.courseID;
    createNote.title = @"创建笔记";
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:createNote];
    createNote.noteCompletionBlock = ^(NSString *note){
        detailNodeView.note = note;
    };
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)detailNodeView:(HLDetailNodeView *)detailNodeView editNote:(NSString *)note
{
    HLCreateNoteController *createNote = [[HLCreateNoteController alloc] init];
    createNote.title = @"编辑笔记";
    createNote.note_course_id = self.courseID;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:createNote];
    createNote.note = note;
    createNote.noteCompletionBlock = ^(NSString *note){
        detailNodeView.note = note;
    };
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)detailNodeView:(HLDetailNodeView *)detailNodeView deleteNote:(NSString *)note
{
    [self requestDeleteNote:note detailNodeView:detailNodeView];
}

/**** 删除笔记 请求 ****/
- (void)requestDeleteNote:(NSString *)note detailNodeView:(HLDetailNodeView *)detailNodeView
{
    [MBProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestEditNotes signStr];
    params[@"course_id"] = self.courseID;
    params[@"type"] = @"0";
    params[@"content"] = note;
    params[@"user_id"] = [HLUserInfo account].userid;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestEditNotes parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            if ([responseObject[@"state"] isEqualToString:@"1"]) {
                [MBProgressHUD showSuccessMessage:@"删除成功"];
                
                /**** 重置 笔记创建 ****/
                [detailNodeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [detailNodeView setUI];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
    
}

@end
